package com.ruoyi.common.utils.report.pic;


import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.encoders.EncoderUtil;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;


/**
 * @author zwz
 * @date 2020/9/30 12:35
 */
public class RadarUtil {
    public static <chart> void main(String args[]) {
        HashMap<String, HashMap<String, Double>> gscores = new HashMap<>();
        HashMap<String, Double> scores = new HashMap<>();
        scores.put("结构人员", 60.0);
        scores.put("结构组织", 70.0);
        scores.put("结构战略", 80.0);
        scores.put("结构结构", 90.0);
        scores.put("结构战略2", 80.0);
        scores.put("结构结构3", 90.0);
        gscores.put("结构人员", scores);


        // 在SWING中显示
        JFrame jf = new JFrame();
        jf.add(erstelleSpinnenDiagramm(gscores));
        jf.pack();
        jf.setVisible(true);


        // 将JFreeChart保存为图片存在文件路径中
        saveAsFile(gscores, "d:/JfreeChart/MySpiderWebPlot.png", 800, 800);
    }
    public static InputStream bufferedImageToInputStream(HashMap<String, HashMap<String, Double>> gscores) throws IOException {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        OutputStream imOut = new BufferedOutputStream(bs);
        JFreeChart chart=createChart(gscores);

//        chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        BufferedImage bufferedImage = chart.createBufferedImage(800, 800);
        EncoderUtil.writeBufferedImage(bufferedImage, "png", imOut);
        InputStream inputStream = new ByteArrayInputStream(bs.toByteArray());
        return inputStream;
    }

    public static JPanel erstelleSpinnenDiagramm(HashMap<String, HashMap<String, Double>> gscores) {
        JFreeChart jfreechart = createChart(gscores);
        ChartPanel chartpanel = new ChartPanel(jfreechart);
        return chartpanel;
    }

    public static void saveAsFile(HashMap<String, HashMap<String, Double>> gscores, String outputPath, int weight, int height) {
        FileOutputStream out = null;
        try {
            File outFile = new File(outputPath);
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            out = new FileOutputStream(outputPath);
            // 保存为PNG
            ChartUtilities.writeChartAsPNG(out, createChart(gscores), weight, height);

            // 保存为JPEG
            // ChartUtilities.writeChartAsJPEG(out, chart, 500, 400);
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // do nothing
                }
            }
        }
    }

    public static JFreeChart createChart(HashMap<String, HashMap<String, Double>> gscores) {
        // SpiderWebPlot spiderwebplot1 = new SpiderWebPlot(createDataset()); //SpiderWebPlot是Jfreechart自带的
        //不用Jfreechart自带的SpiderWebPlot，继承SpiderWebPlot重写MySpiderWebPlot
        CalibrationSpiderWebPlot spiderwebplot = new CalibrationSpiderWebPlot(createDataset(gscores), 100, true);
        //边框颜色
//            spiderwebplot.setOutlinePaint(new Color(0xBD23D1));
//            spiderwebplot.setSeriesPaint(new Color(0xBD23D1));
        //设置内环颜色
        spiderwebplot.setSeriesPaint(0, new Color(0xBD23D1));
        //设置外环颜色
        spiderwebplot.setSeriesPaint(1, new Color(0x44D160));
        spiderwebplot.setLabelFont(new Font("宋体", Font.PLAIN, 18));


        //设置扇形分隔线颜色
//            spiderwebplot.setAxisLinePaint(new Color(0xD15942));
        //设置刻度线前缀
//            spiderwebplot.setLablePrefix("len-");
        //设置标签颜色
//            spiderwebplot.setLabelFont(new Font("微软雅黑",0,7));
        //设置刻度线以及大类名称颜色
//            spiderwebplot.setLabelPaint(new Color(0xD14A08));
        JFreeChart jfreechart = new JFreeChart("", TextTitle.DEFAULT_FONT, spiderwebplot, false);
        jfreechart.setTextAntiAlias(true);
        jfreechart.setAntiAlias(true);
//        LegendTitle legendtitle = new LegendTitle(spiderwebplot);
//        legendtitle.setPosition(RectangleEdge.BOTTOM);
//        jfreechart.addSubtitle(legendtitle);
        return jfreechart;
    }

    private static DefaultCategoryDataset createDataset(HashMap<String, HashMap<String, Double>> gscores) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        for (String group : gscores.keySet()) {
            HashMap<String, Double> scores = gscores.get(group);
            for (String key : scores.keySet()) {
                dataset.addValue(scores.get(key), group, key);
            }
        }

        return dataset;
    }

}

